package com.zgw.his.emr.util.print;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.e4.xwt.forms.medicalControl.print.IPrint;
import org.eclipse.e4.xwt.forms.medicalControl.print.PrintCustom;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.printing.Printer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.e4.xwt.forms.util.ControlUtil;
import com.zgw.his.emr.widget.MedicalPage;

public class MedicalPrint {
	private MedicalPage page;
	private GC gc;
	private PrintCustom custom;

	public MedicalPrint(MedicalPage page, GC gc, Point screenDPI,
			Point printerDPI) {
		this.page = page;
		this.gc = gc;
		gc.setLineWidth(1);
		custom = new PrintCustom(screenDPI, printerDPI);
	}

	public void print() {
		if (page == null || gc == null || gc.isDisposed())
			return;
		List<Control> list = new ArrayList<Control>();
		// 获得所有控件
		MedicalPage
				.getCompositeAllControls((Composite) page.getControl(), list);
		for (Control c : list) {
			if (c instanceof IPrint) { // 控件已实现打印接口
				((IPrint) c).print(gc, custom);
			} else {
				printControl(c);
			}
		}
	}

	public void dispose() {
		if (gc != null && !gc.isDisposed())
			this.gc.dispose();
	}

	private void printControl(Control c) {
		if (c.getClass() == Label.class) {
			printLabel((Label) c);
		} else if (c.getClass() == Composite.class) {
			printComposite((Composite) c);
		} else if (c.getClass() == Group.class) {
			printGroup((Group) c);
		}
	}

	private void printGroup(Group c) {
		Rectangle rect = custom.convertRectangle(ControlUtil.figureBounds(c));
		gc.drawRectangle(rect);
		String text = c.getText();
		if (text != null && !text.equals("")) {
			Point p = gc.stringExtent(text);
			int x = rect.x + custom.convertX(6);
			int y = rect.y - (p.y / 2);
			gc.drawString(text, x, y);
		}
	}

	private void printComposite(Composite c) {
		if ((c.getStyle() & SWT.BORDER) > 0) {
			Rectangle rect = ControlUtil.figureBounds(c);
			gc.drawRectangle(custom.convertRectangle(rect));
		}
	}

	private void printLabel(Label label) {
		String textStr = label.getText();
		Rectangle rect = ControlUtil.figureBounds(label);
		Device device = Display.getDefault();
		Font font = custom.convertFont(label.getFont());
		Color foreground = new Color(device, label.getForeground().getRGB());
		Color background = new Color(device, label.getBackground().getRGB());
		gc.setFont(font);
		gc.setForeground(foreground);
		gc.setBackground(background);
		int x = custom.convertX(rect.x);
		int y = custom.convertY(rect.y);
		int height = custom.convertX(rect.height);
		int width = custom.convertX(rect.width);
		int align = label.getAlignment();
		if ((label.getStyle() & SWT.SEPARATOR) > 0) { // 分割线label
			int lineWidth = gc.getLineWidth();
			gc.drawRectangle(new Rectangle(x, y, width - lineWidth, height
					- lineWidth));
		}
		if (!textStr.equals("")) {
			String[] strs = textStr.split("\r\n");
			int y2 = 0;
			for (String str : strs) {
				if (y2 == 0) {
					y2 = y2 + y
							+ (height / strs.length - gc.stringExtent(str).y)
							/ 2;
				} else {
					y2 = y2 + gc.stringExtent(str).y;
				}
				if (align == SWT.LEFT) {
					gc.drawString(str, x + custom.xpx, y2);
				} else if (align == SWT.CENTER) {
					gc.drawString(str, x + (width - gc.stringExtent(str).x) / 2
							+ custom.xpx, y2);
				} else if (align == SWT.RIGHT) {
					gc.drawString(str, x
							+ (width - gc.stringExtent(str).x - custom.xpx), y2);
				}
			}
		}
		font.dispose();
		foreground.dispose();
		background.dispose();
	}

	/*
	 * =================================================================
	 * *************** 打印工具方法***************
	 * =================================================================
	 */

	/**
	 * 打印单页
	 * 
	 * @param page
	 *            打印页数据
	 **/
	public static void printPage(MedicalPage page) {
		Display display = Display.getDefault();
		Shell shell = display.getActiveShell();
		Printer printer = new Printer(Printer.getDefaultPrinterData());
		Point screenDPI = display.getDPI();
		Point printerDPI = printer.getDPI();
		int xpx = printerDPI.x / screenDPI.x;
		Rectangle trim = printer.computeTrim(0, 0, 0, 0);
		if (printer.startJob("print test")) {
			if (printer.startPage()) {
				GC gc = new GC(printer);
				MedicalPrint print = new MedicalPrint(page, gc, screenDPI,
						printerDPI);
				print.print();
				gc.dispose();
				printer.endPage();
			}
		}
		printer.endJob();
		printer.dispose();

	}
}
